<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Throwable;

/**
 * Class ErrorException
 * @package App\Exceptions
 */
class ErrorException extends Exception
{

    protected $extendData;
    /**
     * ErrorException constructor.
     * @param string $message
     * @param int $code
     * @param Throwable|null $previous
     * @param array $extendData 用于传输错误数据
     */
    public function __construct($message = "", $code = 201, array $extendData=[],Throwable $previous = null)
    {
        $this->extendData = $extendData;
        parent::__construct($message, $code, $previous);
    }

    /**
     * render
     * @param Request $request
     * @return JsonResponse
     */
    public function render(Request $request): JsonResponse
    {
        return fail($this->getCode() ?: 201,$this->getMessage(),$this->extendData);
    }

}
